<template>
    <div class="box_bg">
        <h2 class="box_tit">
            <i class="icon icon1"></i>培养计划
            <i class="reti"></i>
        </h2>
        <!-- 内容区域  开始 -->
        <div class="components_con">
            <!-- 查询开始 -->
            <div class="components_searchs" @keyup.enter="queryTable">
                <span class="tit">名称：</span>
                <el-input v-model="table.queryParams.name" auto-complete="off">
                </el-input>


                <el-button type="primary" icon="el-icon-search" size="mini" @click="queryTable" style="margin-left:7px;">查询</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
            </div>
        </div>
        <!-- components_btns -->
        <div class="components_btns clearfix">
            <div class="fl">
                <el-button type="primary" v-has="'teachMaterials:add'" size="mini" icon="el-icon-plus" @click="showAddDialog">添加</el-button>
            </div>
        </div>
        <!-- components_btns -->
        <div class="scrollBarX">
            <vue-table
                :api-url="table.apiUrl"
                :fields="table.fields"
                :appendParams="table.queryParams"
                :content="self"
                ref="teachMaterialsTable">
            </vue-table>
        </div>

        <teach-materials-add ref="teachMaterialsAdd"></teach-materials-add>
        <teach-materials-edit ref="teachMaterialsEdit"></teach-materials-edit>
        <teach-materials-detail ref="teachMaterialsDetail"></teach-materials-detail>
    </div>
</template>

<script>
    import Vue from 'vue'
    import { mapGetters } from 'vuex'
    import moment from 'moment'
    import teachMaterialsAdd from './teach-materials-add'
    import teachMaterialsEdit from './teach-materials-edit'
    import teachMaterialsDetail from './teach-materials-detail'
    export default {
        components: {
            teachMaterialsAdd,
            teachMaterialsEdit,
            teachMaterialsDetail,
        },
        data() {
            return {
                self: this,
                // 班级表格
                table: {
                    apiUrl: api.teachMaterials.query,
                    queryParams: {
                        name:null,
                    },
                    fields: [
                        {
                            prop: "__index",
                            label: "序号",
                        },
                        {
                            prop: "name",
                            label: "名称",
                        },
                        {
                            prop: "submitTime",
                            label: "上传时间",
                            formatter:function(row, column, cellValue){
                                return moment(cellValue).format('YYYY-MM-DD HH:mm:ss');
                            }
                        },
                        {
                            prop: "__action",
                            label: "操作",
                            width: '180',
                            actions: [
                                {
                                    label: "详情",
                                    type: "edit",
                                    auth: 'teachMaterials:view',
                                    func: function(vm, row) {
                                        vm.$refs.teachMaterialsDetail.showDialog(row.id);
                                    }
                                },
                                {
                                    label: "编辑",
                                    type: "edit",
                                    auth: 'teachMaterials:edit',
                                    func: function(vm, row) {
                                        vm.$refs.teachMaterialsEdit.showDialog(row.id);
                                    }
                                },
                                {
                                    label: "删除",
                                    type: "delete",
                                    auth: 'teachMaterials:delete',
                                    func: function(vm, row) {
                                        vm.deleteById(row.id);
                                    }
                                },
                            ]
                        },
                    ],
                },
            };
        },
        created() {
            // 院校树初始化 获取全部院校列表数据
            this.getSchoolList();
            // 查询年级列表数据
            this.getGradeByDicts();
            // 获取学期列表数据
            this.getSemesterListByDicts();
        },
        mounted() {
        },
        computed:{
            ...mapGetters({
                schoolList: 'getSchoolList',
            })
        },
        watch: {
            filterText(val) {
                this.$refs.schoolTree.filter(val);
            }
        },
        methods: {
            // 获取院校数据
            getSchoolList() {
                this.$store.dispatch("getSchoolList");
            },
            getSecondSchoolList(schoolId) {
                let vm = this;
                vm.axios.get(api.getSecondSchoolList,{
                    params: {
                        schoolId: schoolId
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.secondSchoolList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            getProfessionList(schoolId, secondSchoolId) {
                let vm = this;
                vm.axios.get(api.getProfeesionList, {
                     params: {
                        schoolId: schoolId,
                        secondSchoolId: secondSchoolId
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.professionList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 查询年级(来自数据字典)
            getGradeByDicts(){
                let vm = this;
                vm.axios.get(api.dictsTypesSubListUrl, {
                     params: {
                        type: "grade",
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.gradeList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 查询学期列表(来自字典)
            getSemesterListByDicts(){
                let vm = this;
                vm.axios.get(api.dictsTypesSubListUrl, {
                     params: {
                        type: "semester_type",
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.semesterList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 根据id删除
            deleteById(id) {
                let vm = this;
                vm.$confirm('是否删除该条记录！', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    vm.axios.delete(api.teachMaterials.deleteById + id)
                        .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    message: response.data.msg,
                                    type: 'success',
                                });
                                vm.$refs.teachMaterialsTable.$emit('vuetable:current-refresh')
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                }, () => {})
            },
            // 打开新增培养方案dialog
            showAddDialog() {
                this.$refs.teachMaterialsAdd.showDialog();
            },
            // 打开生成培养方案dialog
            showEditDialog(){
                this.$refs.teachMaterialsEdit.showDialog();
            },
            // 生成培养计划
            generatePlan() {
                let vm = this;
                vm.axios.get(api.trainPlan.generate, {
                     params: {
                        type: "semester_type",
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.semesterList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 导出培养计划
            exportPlan() {
                let href = config.BASE_URL + api.trainPlan.exportExcel;
                href += "?Authorization=" + localStorage.getItem("userToken");
                window.location.href = href;
            },
            changeSchool(schoolId){
                this.table.queryParams.secondSchoolId = null;
                this.table.queryParams.professionId = null;
                this.getSecondSchoolList(schoolId);
            },
            changeSecondSchool(secondSchoolId){
                this.table.queryParams.professionId = null;
                this.getProfessionList(this.table.queryParams.schoolId, secondSchoolId);
            },
            changeProfession(professionId){
                this.getGradeList(professionId);
            },
            // 查询按钮
            queryTable() {
                this.$refs.teachMaterialsTable.$emit('vuetable:refresh')
            },
            // 重置
            reset(){
                for(let key in this.table.queryParams){
                    this.table.queryParams[key] = null;
                }
                this.secondSchoolList = [];
                this.professionList = [];
                this.$refs.teachMaterialsTable.$emit('vuetable:refresh')
            },
        },
    };
</script>
<style scoped>
    .text {
        font-size: 14px;
    }
    .item {
        margin-bottom: 18px;
    }
    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }
    .clearfix:after {
        clear: both;
    }
</style>


